package com.jscloud.sparksql

import org.apache.spark.sql.{DataFrame, SparkSession}

object Demo3forjson {
  def main(args: Array[String]): Unit = {
    //创建sparksession
    val spark = SparkSession.builder()
      .appName("demo3")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .master("local[*]")
      .getOrCreate()

    spark.sparkContext.setLogLevel("warn")

    //情况一：text
    //    val df: DataFrame = spark.read.text(this.getClass.getClassLoader.getResource("person.json").getPath)
    //情况二：json
    val df: DataFrame = spark.read.json(this.getClass.getClassLoader.getResource("person.json").getPath)
    
    df.printSchema

    /** 情况一：
     * root
     * |-- value: string (nullable = true)
     * */
    /** 情况二：
     * root
     * |-- age: long (nullable = true)
     * |-- name: string (nullable = true)
     */
    println("-----")
    df.show()

    /** 情况一：
     * +--------------------+
     * |               value|
     * +--------------------+
     * |  {"name":"Michael"}|
     * |{"name":"Andy", "...|
     * |{"name":"Justin",...|
     * +--------------------+
     * */
    /** 情况二
     * +----+-------+
     * | age|   name|
     * +----+-------+
     * |null|Michael|
     * |  30|   Andy|
     * |  19| Justin|
     * +----+-------+
     */
    spark.stop()
  }
}